package com.wjj.sixinone.service.impl;

import cn.hutool.core.util.IdUtil;

import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.log.TPServerLogs;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.common.util.TPServerBeanUtil;
import com.wjj.sixinone.handler.MailHandler;
import com.wjj.sixinone.service.MailService;
import com.wjj.sixinone.service.minio.MinioFileService;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
public class MailServiceImpl implements MailService {
    /**
     * springboot专门发送邮件接口
     */
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    MailHandler mailHandler;
    @Autowired
    MinioFileService minioFileService;
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    MongoTemplate mongoTemplate;


    public MailServiceImpl(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public Result sendMail(String to) {
        if (to==null){
            throw new GlobalException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        if (redisTemplate.opsForValue().get("to" + to) != null) {
            throw new GlobalException(ExceptionEnum.EMAIL_ALREADY_SEND);
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();


        //验证码
        String code = IdUtil.simpleUUID().substring(0, 5);
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

            helper.setFrom(new InternetAddress(mailHandler.getMailUsername()));
            helper.setTo(to);
            helper.setSubject("验证码");

            // HTML 邮件内容
            String htmlContent = "<div style=\"background-color: #f0f0f0; padding: 10px; border-radius: 20px; text-align: center;\">" + "<h2 style=\"color: #333;\">您好，欢迎使用医院管理系统</h2>" + "<h3 style=\"color: #333;\">您的账户登录验证码是:</h3>" + "<h1 style=\"color: #0066cc;\">" + code + "</h1>" + "<p style=\"color: #666;\">请在两分钟之内使用，逾期将失效</p >" + "</div>";

            helper.setText(htmlContent, true);


            mailSender.send(mimeMessage);
            redisTemplate.opsForValue().set("to" + to, code, 2, TimeUnit.MINUTES);

            return Result.ok();
        } catch (Exception e) {
            TPServerLogs tpServerLogs = TPServerBeanUtil.buildBean(e);

            mongoTemplate.save(tpServerLogs);
            return Result.error(ExceptionEnum.EMAIL_ALREADY_SEND.getStatus(),ExceptionEnum.EMAIL_ALREADY_SEND.getMessage());
        }


    }





}
